<?php
class reportClassAction extends Action
{
    /**
     *采购项目委托计划汇总表
     * @Auther: lv
     * @Email: 578530370@qq.com
     * @Date: 2020/4/9 17:29
     */
	public function commissionSummaryAjax()
    {
        $sdate = $this->request('sdate');
        $edate = $this->request('edate');
        $utype = $this->request('utype');
        $projectadminer = $this->request('projectadminer');
        $where = "1=1";
        $where .= " and c.status = 1 and c.modeid = 94";
        if ($sdate) {
            $where .= " and c.updt >= '".$sdate. " 00:00:01'";
        }
        if ($edate) {
            $where .= " and c.updt <='". $edate. " 23:59:59'";
        }
        if(empty($sdate) && empty($edate)) $where .= " and c.updt >= '".date('Y'). " 00:00:00'";
        if($utype) {
            if ($projectadminer) $where .= " and b.projectadminer=".$projectadminer;
        }else{
            $where .= ' and b.projectadminer = '.$this->adminid;
        }
	    $sql = "select group_concat(b.id) as ids from xinhu_zbcg_approve_sub as a inner join xinhu_zbcg_new_info as b on a.projectid = b.id inner join xinhu_flow_bill as c on b.mid = c.mid where " .$where. " GROUP BY a.mid ORDER BY c.updt";
//        echo $sql;exit;
        $lists = $this->db->getall($sql);
        $total1 = 0;
        $total2 = 0;
        $total3 = 0;
        $total4 = 0;
        $arr = array('工程类', '服务类', '电子卖场', '货物类');
        foreach ($lists as $key => $value) {
            $data = $this->db->getall("select type, sum(budget) as budget from xinhu_zbcg_new_info where id in (". $value['ids'] .") group by type");
            $lists[$key]['name'] = '第'.m('turn')->numTurnChinese($key).'批';
            unset($lists[$key]['ids']);
            $dataArr['工程类'] = 0;
            $dataArr['服务类'] = 0;
            $dataArr['电子卖场'] = 0;
            $dataArr['货物类'] = 0;
            for ($i = 0; $i < count($data); $i++) {
                $dataArr[$data[$i]['type']] = $data[$i]['budget'];
            }
            $lists[$key]['data'] = $dataArr;
            //处理合计
            foreach ($data as $k => $v) {
                switch ($v['type']) {
                    case '工程类':
                        $total1 += $v['budget'];
                        break;
                    case '服务类':
                        $total2 += $v['budget'];
                        break;
                    case '电子卖场':
                        $total3 += $v['budget'];
                        break;
                    case '货物类':
                        $total4 += $v['budget'];
                        break;
                }
            }
        }
        $lists[] = array(
            'name' => '合计',
            'data' => array(
                '工程类' => $total1,
                '服务类' => $total2,
                '电子卖场' => $total3,
                '货物类' => $total4,
            )
        );
        $data = array(
            'lists' => $lists,
            'type' => $arr
        );
        return json_encode($data);
    }

    /**
     * 采购项目委托计划明细表
     * @Auther: lv
     * @Email: 578530370@qq.com
     * @Date: 2020/4/9 10:00
     */
    public function getProjectInfoListAjax(){
        $uid = $this->adminid;
        $whereStr = "1=1";

        $type = $this->request("type");
        $name = $this->request("name");
        $sdate = $this->request("sdate");
        $edate = $this->request("edate");
        $stbudget = $this->request("stbudget");
        $enbudget = $this->request("enbudget");
        $bugtype = $this->request("bugtype");
        $comid = $this->request('comid');
        $utype = $this->request('utype');
        $projectadminer = $this->request("projectadminer");
        //分页
        $page = $this->request('page', 1);
        $limit = $this->request('limit', 15);

        $approveWhere = "1=1";
        $approveWhere .= "  and d.status = 1 and d.modeid = 94";

        if ($sdate) {
            $approveWhere .= " and d.updt >= '".$sdate. " 00:00:01'";
        }
        if ($edate) {
            $approveWhere .= " and d.updt <='". $edate. " 23:59:59'";
        }
        if(empty($sdate) && empty($edate)) $approveWhere .= " and d.updt >= '".date('Y'). " 00:00:00'";
        if ($stbudget) {
            $approveWhere .= " and b.budget >= ".$stbudget;
        }
        if ($enbudget) {
            $approveWhere .= " and b.budget <= ".$enbudget;
        }
        if ($bugtype) {
            $approveWhere .= " and b.buymode = '".$bugtype."'";
        }
        if ($comid) {
            $approveWhere .= " and b.comid = ".$comid;
        }
        if ($projectadminer) {
            if ($projectadminer) $approveWhere .= " and b.projectadminer=".$projectadminer;
        }

        if ($type) {
            $whereStr .= " and b.type='".$type."'";
        }
        if ($name) {
            $whereStr .= " and b.name like '%".$name."%'";
        }
        if($utype) {
            if ($projectadminer) $whereStr .= " and b.projectadminer=".$projectadminer;
        }else{
            $whereStr .= ' and b.projectadminer = '.$this->adminid;
        }
        if ($uid) {
            $sql ="select b.id from [Q]zbcg_new_info as b left join [Q]company as c on b.comid=c.id left join [Q]admin as a on a.id = b.projectadminer inner join [Q]flow_bill as d on b.mid = d.mid where " . $whereStr .' and '. $approveWhere;
            $listSql = "select b.budget,b.funds_type,b.name,b.detail,b.type,b.catalogue,b.buymode,b.gathernote,b.proxygov,c.name as companyname,a.name as projecterName,d.updt from [Q]zbcg_new_info as b left join [Q]company as c on b.comid=c.id left join [Q]admin as a on a.id = b.projectadminer inner join [Q]flow_bill as d on b.mid = d.mid where " . $whereStr .' and '. $approveWhere .' limit '.($page-1)*$limit.','.$limit;
//            echo $listSql;exit;
            $number = $this->db->getall($sql);
            $list = $this->db->getall($listSql);
        }

        $bacarr	= array(
            'totalCount'=> count($number),
            'rows'		=> $list
        );
        return json_encode($bacarr);
    }
}